//
//  SettingViewController.m
//  Drivers
//
//  Created by Hunk on 10-11-16.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "SettingViewController.h"
#import "WriteFile.h"

@interface SettingViewController (Private)

- (void)addActionSheet:(NSString*)strTitle;

@end


#define TITLE_REVERTWRONGQUESTIONS      @"确定重置错题回顾数据?" 
#define TITLE_REVERTSTRENGTHENINGMEMORY @"确定重置强化记忆数据?"
#define TITLE_REVERTALLDATA             @"确定重置本机所有数据?"



@implementation SettingViewController
@synthesize m_pRevertWrongQuestionsButton;
@synthesize m_pRevertStrengtheningMemoryButton;
@synthesize m_pRevertAllDataButton;

 // The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad 
{
    [super viewDidLoad];
	
	// Set title
	[self.navigationController.visibleViewController setTitle:@"应用设置"];
    
    UIImage *image = [UIImage imageNamed:@"cell_background.png"];
    [_bgImageView setImage:[image stretchableImageWithLeftCapWidth:5 topCapHeight:5]];
    
    [UIView viewLayerCorner:m_pRevertWrongQuestionsButton];
    [UIView viewLayerCorner:m_pRevertStrengtheningMemoryButton];
    [UIView viewLayerCorner:m_pRevertAllDataButton];
}

#pragma mark -
#pragma mark Revert wrong questions button handle
- (IBAction)revertWrongQuestionsButtonHandle:(id)sender
{		
	// Add action sheet
	[self addActionSheet:TITLE_REVERTWRONGQUESTIONS];
}

#pragma mark -
#pragma mark Revert strengthening memory button handle
- (IBAction)revertStrengtheningMemoryButtonHandle:(id)sender
{
	// Add action sheet
	[self addActionSheet:TITLE_REVERTSTRENGTHENINGMEMORY];
}

#pragma mark -
#pragma mark Revert all data button handle
- (IBAction)revertAllDataButtonHandle:(id)sender
{
	// Add action sheet
	[self addActionSheet:TITLE_REVERTALLDATA];	
}

#pragma mark -
#pragma mark Add action sheet
- (void)addActionSheet:(NSString*)strTitle
{
	// open a dialog with an OK and cancel button
	UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:strTitle
															 delegate:self 
													cancelButtonTitle:@"取消" 
											   destructiveButtonTitle:@"确定" 
													otherButtonTitles:nil];
	
	actionSheet.actionSheetStyle = UIActionSheetStyleDefault;
	
	// show from our table view (pops up in the middle of the table)
	[actionSheet showInView:self.view];
	[actionSheet release];
}

#pragma mark -
#pragma mark Add action sheet delegate function
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
	if([actionSheet.title isEqualToString:TITLE_REVERTWRONGQUESTIONS])
	{
		if(0 == buttonIndex)
		{
			WriteFile* writeFile = [[WriteFile alloc] init];
			
			[writeFile DeleteFile:@"WrongQuestion.sg"];
			
			[writeFile DeleteFile:@"WrongQuestion.plist"];
			
			[writeFile CloseFile:@"WrongQuestion.sg"];
			[writeFile CloseFile:@"WrongQuestion.plist"];
			
			[writeFile release];
		}
		else if(1 == buttonIndex)
		{
			NSLog(@"Wrong question cancel!\n");
		}
	}
	else if([actionSheet.title isEqualToString:TITLE_REVERTSTRENGTHENINGMEMORY])
	{
		if(0 == buttonIndex)
		{
			WriteFile* writeFile = [[WriteFile alloc] init];
			
			[writeFile DeleteFile:@"StrengtheningMemory.sg"];
			
			[writeFile DeleteFile:@"StrengtheningMemory.plist"];
			
			[writeFile CloseFile:@"StrengtheningMemory.sg"];
			[writeFile CloseFile:@"StrengtheningMemory.plist"];
			
			[writeFile release];
		}
		else if(1 == buttonIndex)
		{
			NSLog(@"Strengthening memory cancel!\n");
		}
	}
	else if([actionSheet.title isEqualToString:TITLE_REVERTALLDATA])
	{
		if(0 == buttonIndex)
		{
			WriteFile* writeFile = [[WriteFile alloc] init];
			
			[writeFile DeleteFile:@"WrongQuestion.sg"];
			
			[writeFile DeleteFile:@"WrongQuestion.plist"];
			
			[writeFile DeleteFile:@"StrengtheningMemory.sg"];
			
			[writeFile DeleteFile:@"StrengtheningMemory.plist"];
			
			[writeFile CloseFile:@"WrongQuestion.sg"];
			[writeFile CloseFile:@"WrongQuestion.plist"];
			[writeFile CloseFile:@"StrengtheningMemory.sg"];
			[writeFile CloseFile:@"StrengtheningMemory.plist"];
			
			[writeFile release];			
		}
		else if(1 == buttonIndex)
		{
			NSLog(@"All data cancel!\n");
		}
	}
}


#pragma mark -
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [super dealloc];
}


@end
